<?php
/**
 * Created by PhpStorm.
 * User: 许文能
 * Date: 2016/8/22 0022
 * Time: 上午 9:29
 */
namespace Admin\Controller;

use Common\Library\Verify;
use Think\Controller;

class SystemController extends Controller
{

    public function login_action(){
        $this->display('login');
    }

    //显示图片验证码
    Public function verify(){
        $verify = new Verify();
        $_SESSION['Verify'] = $verify->Code;
        $verify->ShowImage();
    }

    //用户登录处理
    public function login(){
        // 接收用户名和密码和验证码
        $username= I('post.user');
        $password=I('post.psw');
        $verify = I('post.verify');

        //验证码校验
        $verify = strtolower($verify);
        $session_verify = strtolower($_SESSION['Verify']);
        if($session_verify!=$verify){
            $this->error("验证码不正确");
        }
        
        // 校验用户名密码格式
        if(!preg_match('/^[a-zA-Z0-9_]{3,16}$/',$username)) {
            $this->error('用户名格式不正确');
        }

        // 获取用户模型对象
        $user = D('admin');
        //根据用户名查询用户信息
        $date = $user->where(['user'=>$username])->select();

        if(!empty($date)){
            if($date[0]['psw'] == $password){
                //密码正确
                $_SESSION['user']=$date[0]['id'];
                $this ->success('登录成功','/index.html');
            }
            else{
                //密码错误
                $this ->error('密码错误','/');
            }
        }
        else{
            $this ->error('用户名错误','/');
        }

    }

    public function top(){
        //获取数据库用户名信息
        $id = $_SESSION['user'];
        $user = D('admin');
        $data = $user->where(['id'=>$id])->select();
        $name = $data[0]['user'];
        $this->assign('name',$name);
        $this->display('top');

    }

    public function zhuX(){
        unset($_SESSION['user']);
        $this->success('成功退出','/login.html');
    }

}


